package kd.bos.kdec.pincar;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.filter.FilterParameter;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Image;
import kd.bos.form.control.Label;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 拼车圈子-低碳排行榜
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobCircle extends AbstractFormPlugin {

	private String[] buttons = new String[] { "imag_help_cgsq", "dingbill", "pincarclick", "myclick","morepai" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		Map<String, Object> formId = new HashMap<>();
		if (StringUtils.equals("imag_help_cgsq", key)) {// 问号提示
			formId.put("formId", "kdec_pincar_dthelp");
			this.showForm(formId);
		} else if (StringUtils.equals("dingbill", key)) {// 行程单列表
			formId.put("formId", "kdec_pincarbilllist");
			this.showForm(formId);
		} else if (StringUtils.equals("pincarclick", key)) {// 首页跳转
			formId.put("formId", "kdec_pincar");
			this.showForm(formId);
		} else if (StringUtils.equals("myclick", key)) {// 我的 跳转
			formId.put("formId", "kdec_pincar_my");
			this.showForm(formId);
		}else if (StringUtils.equals("morepai", key)) {// 更多排名
			formId.put("formId", "kdec_pincar_reportlist");
			this.showForm(formId);
		}
	}

	@Override
	public void beforeClick(BeforeClickEvent evt) {
		super.beforeClick(evt);

	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		long orgId = RequestContext.get().getOrgId();
		long userId = Long.parseLong(RequestContext.get().getUserId());
		QFilter orfilter = new QFilter("orgfield", QCP.equals, orgId);
		DynamicObject[] objs = BusinessDataServiceHelper.load("kdec_pincar_reportbill", "creator,kmsnum",
				orfilter.toArray(), "kmsnum desc");//按照公里积分倒序排名
		int mypm = 1;
		for (DynamicObject dyObj : objs) {//计算我的排名(算法很烂可以参考人人差旅)
			DynamicObject creator = (DynamicObject)dyObj.get("creator");
			Long id = creator.getLong("masterid");
			if (userId != id) {
				mypm = mypm + 1;
			}else if (userId == id) {
				break;
			}
		}
		((Label) this.getControl("mypm")).setText(mypm + "");
		if (null != objs && objs.length > 0 && null != objs[0].get("creator")) {//前三名才有资格有头像展示
			DynamicObject dyobj = (DynamicObject) objs[0].get("creator");
			String url = dyobj.getString("picturefield");
			((Image) this.getControl("image_avatar")).setUrl(url);
			if (null != objs && objs.length > 1 && null != objs[0].get("creator")) {
				DynamicObject dyobj1 = (DynamicObject) objs[1].get("creator");
				String url1 = dyobj1.getString("picturefield");
				((Image) this.getControl("image_avatartwo")).setUrl(url1);
				if (null != objs && objs.length > 2 && null != objs[0].get("creator")) {
					DynamicObject dyobj2 = (DynamicObject) objs[2].get("creator");
					String url2 = dyobj2.getString("picturefield");
					((Image) this.getControl("image_avatarthrid")).setUrl(url2);
				}
			}
			QFilter filter = new QFilter("creator", QCP.equals, userId);
			DynamicObject object = BusinessDataServiceHelper.loadSingleFromCache("kdec_pincar_reportbill", "kmsnum",
					filter.toArray());
			Label label = this.getControl("mynum");
			label.setText(object.getString("kmsnum"));//我的积分
		} else {// 无数据显示
			this.getView().setVisible(true, "flexpanelap4");
			this.getView().setVisible(false, "flexpanelap7");
			this.getView().setVisible(false, "flexpanelap2");
		}
	}

	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
		// 根据组织隔离数据
		long orgId = RequestContext.get().getOrgId();
		// 加载单据列表添加筛选条件
		BillList billList = this.getControl("billlistap");
		FilterParameter filterParameter = new FilterParameter();
		QFilter filter = new QFilter("orgfield", QCP.equals, orgId);
		filterParameter.setFilter(filter);
		billList.setFilterParameter(filterParameter);
	}
	
	/**
	 * 页面跳转通用方法
	 * 
	 * @param formId
	 *            页面标识
	 */
	private void showForm(Map<String, Object> formId) {
		FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
		showParameter.getOpenStyle().setShowType(ShowType.Floating);
		this.getView().showForm(showParameter);
	}
}
